from pyecharts.charts import Map
import json
from pyecharts import options as opts

with open("疫情.txt", "r", encoding="utf-8") as f:
    data = f.read()
data = json.loads(data)
data = data["areaTree"][0]["children"]
for e in data:
    if e["name"] == "河南":
        data = e["children"]
print(data)
data_list = []
for e in data:
    num = e["total"]["confirm"]
    data_list.append([e["name"], num])
print(data_list)
city_mapping = {
    '郑州': '郑州市',
    '商丘': '商丘市',
    '开封': '开封市',
    '驻马店': '驻马店市',
    '安阳': '安阳市',
    '许昌': '许昌市',
    '三门峡': '三门峡市',
    '漯河': '漯河市',
    '周口': '周口市',
    '南阳': '南阳市',
    '信阳': '信阳市',
    '济源示范区': '济源市',
    '新乡': '新乡市',
    '焦作': '焦作市',
    '平顶山': '平顶山市',
    '洛阳': '洛阳市',
    '濮阳': '濮阳市',
    '鹤壁': '鹤壁市',
    '境外输入': '境外输入',
    '地区待确认': '地区待确认'
}
new_data = [[city_mapping[e[0]], e[1]] for e in data_list]
print(new_data)
map2 = Map()
map2.add("河南疫情地图", new_data, "河南")
map2.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
            {"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
            {"min": 100, "max": 499, "label": "99-499人", "color": "#FF9966"},
            {"min": 500, "max": 999, "label": "499-999人", "color": "#FF6666"},
            {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
            {"min": 10000, "label": "10000以上", "color": "#990033"}
        ]
    )
)
map2.render(path="map7.html")
